package alipay.openplatform.edu.campus.jiuye.api.controller.system;


import alipay.openplatform.edu.campus.jiuye.common.enums.ResultCode;
import alipay.openplatform.edu.campus.jiuye.common.utils.ObjectUtils;
import alipay.openplatform.edu.campus.jiuye.service.dao.ApiResult;
import alipay.openplatform.edu.campus.jiuye.service.iservice.system.ISchoolInfoService;
import alipay.openplatform.edu.campus.jiuye.service.services.system.AreaInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/jiuye/api")
public class SchoolInfoController {


    // 学校SERVICE类
    @Autowired
    ISchoolInfoService iSchoolInfoService;



    //日志操作类
    private static Logger logger = LoggerFactory.getLogger(AreaInfoService.class);

    /**
     * 根据学校名称获取城市列表(分页)
     * @param schoolName  城市名称
     * @return
     */
    @RequestMapping(value = "/getSchoolPage",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getSchoolPage(HttpServletRequest request,

                                 @RequestParam(value = "schoolName", required = true)String schoolName,
                                 @RequestParam(value = "pageNo", required = false,defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", required = false,defaultValue = "10") Integer pageSize

    ) {


        // 1. 验证参数
        if(ObjectUtils.hasNull(schoolName)) {

            return ApiResult.build("-1","参数传递不正确!");
        }

        //2. 返回结果
        try {
            return iSchoolInfoService.getSchoolPage(schoolName,pageNo,pageSize);

        } catch (Exception e) {
            logger.info("根据学校名称获取城市列表|SchoolInfoController|getSchoolPage|params:schoolName={},pageNo={},pageSize={},e={}", schoolName,pageNo,pageSize,e);
            return new ApiResult().build(ResultCode.SYSTEM_EXCEIPTION.getCode(), ResultCode.SYSTEM_EXCEIPTION.getName());

        }
    }


    /**
     * 根据学校编号删除城市信息
     * @param schoolId  城市编号
     * @return
     */
    @RequestMapping(value = "/delSchool")
    @ResponseBody
    public ApiResult delSchool(@RequestParam(value = "schoolId", required = true)String schoolId)
    {


        // 1. 验证参数
        if(ObjectUtils.hasNull(schoolId)) {

            return ApiResult.build("-1","参数传递不正确!");
        }

        //2. 返回结果
        try {
            return iSchoolInfoService.delSchool(schoolId);

        } catch (Exception e) {
            logger.info("根据城市编号删除城市信息|SchoolInfoController|delSchool|params:schoolId={},e={}", schoolId,e);
            return new ApiResult().build(ResultCode.SYSTEM_EXCEIPTION.getCode(), ResultCode.SYSTEM_EXCEIPTION.getName());

        }
    }
}